package shiqi.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import shiqi.chain.code.*;
import shiqi.chain.handler.ChainFirstHandler;
import shiqi.chain.handler.ChainSecondHandler;
import shiqi.chain.version2.OrderService;
import shiqi.dto.ReqDTO;
import shiqi.dto.ResDTO;

/**
 * @author shiqi
 * @version 1.0.0
 * @ClassName HnadlerController.java
 * @Description
 * @createTime 2022-05-10
 */
@RestController
@RequestMapping("/handler")
public class HandlerController {

    @Autowired
    private SpringContentUtils scu;

    @Autowired
    private OrderService orderService;


    @GetMapping("/chain")
    public String chain() {
        ReqDTO request = new ReqDTO();
        ResDTO resDTO = new ResDTO();
        Bootstrap bootstrap=new Bootstrap();
        bootstrap.childHandler(new HandlerInitializer(request,resDTO) {
            @Override
            protected void initChannel(Pipeline pipeline) {
                pipeline.addLast(scu.getHandler(ChainFirstHandler.class));
                pipeline.addLast(scu.getHandler(ChainSecondHandler.class));
            }
        });
        resDTO = (ResDTO)bootstrap.execute();
        System.out.println(resDTO.getName());
        return "success";
    }

    @GetMapping("/chain1")
    public String chain1() {
        ContextResponse response = orderService.execute(new ContextRequest());
        System.out.println(response);
        return "success";
    }
}
